<?php
namespace Index\Controller;
use Think\Controller;
class FocusController extends Controller {
    /**
     *关注问题
     */
    public function indexAction(){
        $qid = I("get.id");
        $uid = I("session.uid");
        $model = M();
        $sql = "select * from focus where qid = $qid and uid = $uid ";
        $exist = $model -> query($sql);
        if(empty($exist)){
            $sql = "insert into focus (uid,qid) values('$uid','$qid')";
            $model->execute($sql);
            $sql = "select attention from question where id = $qid ";
            $attentionData = $model -> query($sql);
            $attention = $attentionData[0]['attention'] + 1;
            $sqlAttention = "update question set attention = '$attention' where id = $qid";
            $model->execute($sqlAttention);
            $this->success('关注成功',U("Index/Question/show/id/$qid"),1);
        }else{
            $sql = "delete from focus where qid = $qid and uid = $uid ";
            $model->execute($sql);
            $sql = "select attention from question where id = $qid ";
            $attentionData = $model -> query($sql);
            $attention = $attentionData[0]['attention'] - 1;
            $sqlAttention = "update question set attention = '$attention' where id = $qid";
            $model->execute($sqlAttention);
            $this->success('取消关注成功',U("Index/Question/show/id/$qid"),1);
        }

    }

    /**
     *关注人
     */
    public function attendAction(){
        $aid = I("get.id");
        $uid = I("session.uid");
        $model = M();
        $sql = "select * from attention where aid = '{$aid}' and uid = '{$uid}' ";
        $exist = $model -> query($sql);
        if(empty($exist)){
            $sql = "insert into attention(aid,uid) values('{$aid}','{$uid}')";
            if($model->execute($sql)) $this->success('关注成功',U("Index/People/index/id/$aid"),1);
        }else{
            $sql = "delete from attention where aid = '{$aid}' and uid = '{$uid}' ";
            if($model->execute($sql)) $this->success('取消关注成功',U("Index/People/index/id/$aid"),1);
        }
    }

}